package com.yn.club.auth.application.controller;

import com.google.common.base.Preconditions;
import com.yn.club.auth.domain.entity.AuthUserDO;
import com.yn.club.auth.application.dto.AuthUserDTO;
import com.yn.club.auth.domain.service.AuthUserDomainService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import com.yn.club.common.util.R;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import com.yn.club.auth.application.convert.AuthUserApplicationConverter;
import com.yn.club.auth.application.request.AuthUserQueryRequest;
import com.yn.club.auth.application.request.AuthUserSaveRequest;

/**
 * 用户信息表(AuthUser)表控制层
 *
 * @author YN
 * @since 2025-01-22 12:33:46
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("user")
public class AuthUserController {
    /**
     * 服务对象
     */
    private final AuthUserDomainService authUserDomainService;
    private final AuthUserApplicationConverter authUserApplicationConverter;

    /**
     * 分页查询数据
     *
     * @param pageReq 分页参数
     * @param query   查询条件
     * @return 所有数据
     */
    @GetMapping
    public R<PageResp<AuthUserDTO>> queryPage(PageReq pageReq, AuthUserQueryRequest query) {
        AuthUserDO authUserDO = authUserApplicationConverter.convertQueryRequestToDo(query);
        PageResp<AuthUserDO> page = authUserDomainService.page(pageReq, authUserDO);
        
        List<AuthUserDTO> authUserDTOS = authUserApplicationConverter.convertDoToDto(page.getList());
        return R.ok(PageResp.of(page,authUserDTOS));
    }

    /**
     * 通过查询单条数据
     *
     * @param userName 名称
     * @return 单条数据
     */
    @GetMapping("getUserInfo/{userName}")
    public R<AuthUserDTO> selectOne(@PathVariable String userName) {
    AuthUserDTO authUserDTO = authUserApplicationConverter.convertDoToDto(
      this.authUserDomainService.getUserInfo(userName));
    return R.ok(authUserDTO);
    }

    /**
     * 用户注册
     *
     * @param authUser 实体对象
     * @return 受影响条数
     */
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody @Validated AuthUserSaveRequest authUser) {
       AuthUserDO authUserDO = authUserApplicationConverter.convertSaveRequestToDo(authUser);
        return R.ok(this.authUserDomainService.insert(authUserDO));
    }

    /**
     * 修改数据
     *
     * @param authUser 实体对象
     * @return 受影响条数
     */
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody AuthUserSaveRequest authUser) {
        AuthUserDO authUserDO = authUserApplicationConverter.convertSaveRequestToDo(authUser);
        return R.ok(this.authUserDomainService.updateById(authUserDO));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 受影响条数
     */
    @DeleteMapping("/delete")
    public R<Boolean> delete(@RequestBody @NotEmpty List<Integer> ids) {
        return R.ok(this.authUserDomainService.delete(ids));
    }

    /**
     * 用户启用/禁用
     */
    @RequestMapping("changeStatus")
    public R<Boolean> changeStatus(@RequestBody AuthUserSaveRequest request) {
        Preconditions.checkNotNull(request.getStatus(), "用户状态不能为空");
        AuthUserDO authUserDO = authUserApplicationConverter.convertSaveRequestToDo(request);
        return R.ok(authUserDomainService.updateById(authUserDO));
    }


}

